﻿using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using SqlSugar;
using System.Web.Http;
using wnet.admin.dbhelper;
using ZR.Model.System;
using wnet.admin.Models;
using System.Collections.Generic;
using wnet.admin.Models.dto;
using System.Linq;
using System.Web.UI.WebControls;
using System;
using ZR.Model.Business;
using wnet.admin.wentcommon;




namespace wnet.admin.Controllers.pcapi
{
    [TokenAuth]
    [Route("system/dict/data/{action}")]
    public class SysDictDataController : ApiController
    {

        private readonly ISqlSugarClient _sqlSugarClient;
        public SysDictDataController()
        {
            _sqlSugarClient = SqlSugarConfig.CreateClient();
        }


        /// <summary>
        /// 根据字典类型查询字典数据信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost]
        public object types(List<SysdictDataDto> dto)
        {

            var dictTypes = dto.Select(s => s.DictType).ToArray();
            var list = _sqlSugarClient.Queryable<SysDictData>().Where(f => f.Status == "0" && dictTypes.Contains(f.DictType)).ToList();
            List<SysdictDataDto> dataVos = new List<SysdictDataDto>();

            foreach (var dic in dto)
            {
                SysdictDataDto vo = new SysdictDataDto()
                {
                    DictType = dic.DictType,
                    ColumnName = dic.ColumnName,
                    List = list.Where(f => f.DictType == dic.DictType).ToList(),
                };
                if (dic.DictType.StartsWith("cus_") || dic.DictType.StartsWith("sql_"))
                {
                    //  return Context.Ado.SqlQuery<SysDictData>(sysDictType?.CustomSql).ToList();

                    var dictInfo = _sqlSugarClient.Queryable<SysDictType>().Where(q => q.DictType == dic.DictType).First();

                    var sqllistdata = _sqlSugarClient.Ado.SqlQuery<SysDictData>(dictInfo.CustomSql).ToList();

                    vo.List.AddRange(sqllistdata);
                }
                dataVos.Add(vo);
            }


            var data = new { code = 200, msg = "成功", data = dataVos };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }







        /// <summary>
        /// 获取
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object list(UserQueryDto dto)
        {

            var exp = Expressionable.Create<SysUser>();

            exp.And(u => u.DelFlag == 0);
            var query = _sqlSugarClient.Queryable<SysUser>()
                .Where(exp.ToExpression())
                .Select((u) => new SysUser
                {
                    UserId = u.UserId.SelectAll(),

                });

            var datalist = query.ToPage(dto);

            var data = new { code = 200, msg = "成功", data = datalist };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }




    }
}
